package com.jt9003.controller;

import com.jt9003.pojo.Drugs;
import com.jt9003.pojo.Template;
import com.jt9003.pojo.Template_drugs;
import com.jt9003.pojo.Usage;
import com.jt9003.service.TemplateService;
import com.jt9003.utils.ShuttleBoxGetData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

//模板管理
@RestController
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    //院内协定方的药品数据
    List<ShuttleBoxGetData> shuttleBoxGetDataList = new ArrayList<>();

    //接收模板管理发来的病例模板
    @RequestMapping("/addTemplateCasehistory")
    public Map addTemplateCasehistory(Template template){
        Map<String,Object> map = new HashMap<>();
        try {
            template.setT_date(new Date()); //记录创建病例的时间
            template.setT_type(40);     //设置模板的类型 40:病历模板
            int count = templateService.addTemplate(template);
            if (count>0){
                map.put("success",true);
                map.put("message","病例模板添加成功");
            }else{
                map.put("success",false);
                map.put("message","添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
            map.put("message","添加异常");
        }
        return map;
    }

    //获取病例模板数据 t_type=40
    @RequestMapping("/getTemplate")
    public Map getTemplate(){
        Map<String,Object> map = new HashMap<>();
        try {
            List<Template> list = templateService.getTemplate();
            if (list.size()>0){
                map.put("success",true);
                map.put("message","查询病历模板数据成功");
                map.put("result",list);
            }else{
                map.put("success",false);
                map.put("message","查询病历模板数据失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
            map.put("message","查询病历模板数据异常");
        }
        return map;
    }

    //查询所有药品(药品状态是可用)
    @RequestMapping("/getDrugs")
    public Map getDrugs(){
        Map<String,Object> map = new HashMap<>();
        try {
            List<Drugs> list = templateService.getDrugs();
            if(list.size()>0){
                map.put("success",true);
                map.put("message","查询药品数据成功");
                map.put("result",list);
            }else{
                map.put("success",true);
                map.put("message","查询药品失败");
            }
        }catch(Exception e){
            e.printStackTrace();
            map.put("success",true);
            map.put("message","查询药品异常");
        }
        return map;
    }

    //接收院内协定方提交的药品数据
    @RequestMapping(value = "/saveDrugsInfo",method = {RequestMethod.POST})
    public int saveDrugsInfo(@RequestBody List<ShuttleBoxGetData> shuttleBoxGetData){
        int count =shuttleBoxGetData.size();
        //清空数据，以便重复提交时修改提交的数据
        if (shuttleBoxGetDataList.size()>0){
            shuttleBoxGetDataList.clear();
        }
        if (count >0){
            shuttleBoxGetDataList=shuttleBoxGetData;
        }
        return count;
    }

    //接收协定方模板数据和调理方数据
    @RequestMapping("/AgreementPartyTemplate")
    @Transactional
    public Map saveAgreementPartyTemplate(Template template){
        Map<String,Object> map = new HashMap<>();
        List<Template_drugs> list= new ArrayList<>();
        try {
            //设置t_type=41 院内协定方   t_type=43 调理方
           if (template.getTemplateNamber()==41){
               template.setT_type(41);
           }else{
               template.setT_type(43);
           }
            template.setT_date(new Date());

            int count = templateService.addTemplate(template);
            if (count>0){
                //获取返回的模板id ,药品id
                int id =template.getId();
                for (ShuttleBoxGetData s:shuttleBoxGetDataList){
                    Template_drugs td=new Template_drugs();
                    td.setDr_id(s.getValue());
                    td.setT_id(id);
                    list.add(td);
                }
                int i = templateService.addTemplate_drugs(list);
                if (i>0){
                    map.put("success",true);
                    map.put("message","院内协定方添加成功");
                    shuttleBoxGetDataList.clear();  //清空保存的数据
                }else{
                    map.put("success",false);
                    map.put("message","药品与模板对应表插入数据失败");
                    throw new Exception("药品与模板对应表插入数据失败");
                }
            }else {
                map.put("success",false);
                map.put("message","院内协定方添加失败");
                throw new Exception("院内协定方添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            shuttleBoxGetDataList.clear();  //清空保存的数据
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return map;
    }

    //查询用法，usage_state=15
    @RequestMapping("/getUsage")
    public Map selectUsage(){
        Map<String,Object> map = new HashMap<>();

        try {
            List<Usage> list = templateService.selectUsage();
            if (list.size()>0){
                map.put("success",true);
                map.put("result",list);
            }else {
                map.put("success",false);
                map.put("message","查询用法列表失败");
            }

        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
            map.put("message","查询用法列表异常");
        }
       return map;
    }

    //查询院内协调方数据
    @RequestMapping("/getAgreementPartyTemplate")
    public Map getAgreementPartyTemplate(){
        Map<String,Object> map = new HashMap<>();
        try {
            List<Template> list = templateService.getAgreementPartyTemplate();
            if (list.size()>0){
                map.put("success",true);
                map.put("data",list);
            }else{
                map.put("success",false);
                map.put("message","查询院内协调方列表失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
            map.put("message","查询院内协调方列表异常");
        }
        return map;
    }

    //删除院内协定方模板  调理方模板
    @RequestMapping("/delTemplate")
    public int delTemplate(int id){
        System.out.println("拿到的id："+id);
        int  count = 0;
        try {
            count = templateService.delTemplate(id);
        }catch(Exception e){
            e.printStackTrace();
            return count;
        }
        return count;
    }

    //删除患者病例模板
    @RequestMapping("/delTemplate2")
    public int delTemplate2(int id){
        System.out.println("拿到的id："+id);
        int  count = 0;
        try {
            count = templateService.delTemplate2(id);
        }catch(Exception e){
            e.printStackTrace();
            return count;
        }
        return count;
    }


    //获取调理方模板数据
    @RequestMapping("/OrganizeTemplate")
    public Map getOrganizeTemplate(){
        Map<String,Object> map = new HashMap<>();
        try {
            List<Template> list = templateService.getOrganizeTemplate();
            int total = templateService.OrganizeTemplateTotal();
            if (list.size()>0&&total!=0){
                map.put("success",true);
                map.put("data",list);
                map.put("count",total);
                map.put("code",0);
            }else {
                map.put("success",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
        }
       return map;
    }

    //接收医嘱模板的数据
    @RequestMapping("/addmedical_advice")
    public int addmedical_advice(Template template){
        int count = 0;
        try {
            template.setT_date(new Date());
            template.setT_type(42);

            count = templateService.addTemplate(template);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    //查询所有医嘱模板
    @RequestMapping("/getMedical_advice")
    public Map getMedical_advice(){
        Map<String,Object> map = new HashMap<>();
        try {
            List<Template> list =templateService.getMedical_advice();
            if (list.size()>0){
                map.put("success",true);
                map.put("data",list);
            }else{
                map.put("success",false);
            }
        }catch(Exception e){
            e.printStackTrace();
            map.put("success",false);
        }
        return map;
    }



}
